﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using SmartMD.DataModel.Entity;
using Techstar.Common;
using Techstar.Dialog;
using SmartMD.DataAccess.Service;

namespace SmartMD.Manager.SysInfo.User
{
    /// <summary>
    /// Interaction logic for UserManagerWindow.xaml
    /// </summary>
    public partial class UserManagerWindow : BaseWindow
    {
        public ObservableCollection<UserInfo> userInfoList;
        //最大用户数
        private int MaxUserNum = 0;
        private int CurrentUserNum = 0;

        public UserManagerWindow()
        {
            InitializeComponent();

            SysInfoEntity sysInfoEntity = DataServiceManager.SysInfoService.GetObjectById("max_user_num");
            if (sysInfoEntity != null)
            {
                MaxUserNum = int.Parse(sysInfoEntity.Value);
            }
            this.RegisterToolbar(this.MyToolbar);

            this.Title = "用户信息维护";

            userInfoList = this.LoadAllUserInfos();
            if (userInfoList == null)
                userInfoList = new ObservableCollection<UserInfo>();
            this.CurrentUserNum = userInfoList.Count;

            lvUserInfo.ItemsSource = userInfoList;
        }

        public static string ModuleName
        {
            get { return "用户维护"; }
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtSearch.Text))
                this.lvUserInfo.ItemsSource = userInfoList;
            else
            {
                var result = new ObservableCollection<UserInfo>();
                foreach (var item in userInfoList)
                {
                    var list = new List<string>
                                   {
                                       item.Name                                       
                                   };
                    if (StringEx.Like(list, txtSearch.Text.Split(new[] { ' ' })))
                        result.Add(item);
                }
                this.lvUserInfo.ItemsSource = result;
            }
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                btnSearch_Click(null, null);
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (lvUserInfo.SelectedItem != null)
            {
                UserInfo userInfo = lvUserInfo.SelectedItem as UserInfo;

                UserInfoEdit view = new UserInfoEdit(this);
                view.userInfo = userInfo;
               
                view.refresh += new UserInfoEdit.Refresh(view_refresh);
                view.ShowDialog();       
            }
            else
            {
                TaskDialogEx.Warning("请选择要编辑的用户!");
            }
        }

        void view_refresh(UserInfo selectItem)
        {
            lvUserInfo.SelectedItem = selectItem;
        }

        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            if (this.CurrentUserNum++ >= this.MaxUserNum)
            {
                TaskDialogEx.Warning("已超过设定的最大用户数，无法继续增加用户!");
                return;
            }
            UserInfo userInfo = new UserInfo();
            userInfo.Id = null;
            UserInfoEdit view = new UserInfoEdit(this);
            view.userInfo = userInfo;
            view.ShowDialog();
           // view.DatabaseMetaInfo = databaseMetaInfo;
           
            view.refresh += new UserInfoEdit.Refresh(view_refresh);
           
        }

        private void btnDele_Click(object sender, RoutedEventArgs e)
        {
            if (lvUserInfo.SelectedItem != null)
            {
                UserInfo item = lvUserInfo.SelectedItem as UserInfo;
                if (MessageBox.Show("是否删除当前用户?", "提示信息", MessageBoxButton.YesNo, MessageBoxImage.Question) == (MessageBoxResult)System.Windows.Forms.DialogResult.Yes)
                {
                    DataServiceManager.UserService.DeleteObject(item);
                    userInfoList.Remove(item);
                    this.CurrentUserNum --;
                }
            }
            else
            {
                TaskDialogEx.Warning("请选择要删除的用户!");
            }
        }

        private ObservableCollection<UserInfo> LoadAllUserInfos()
        {
            var result = DataServiceManager.UserService.GetAllUserInfos();
            return result;
        }
    }
}
